<template>
  <div>
    <el-dialog v-model="model" @close="closedDialog" width="600px" :title="$t('flow.delegate')">
      <el-form ref="taskFormRef" :model="taskFrom" label-width="80px" :rules="rules">
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('task.actionUser')" prop="userId">
              <DeptSelect v-model="taskFrom.userId" filterable clearable >
              </DeptSelect>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="$t('task.comment')" prop="comment">
              <el-input v-model="taskFrom.comment"  :rows="4" type="textarea"
                :placeholder="$t('task.commentPlaceholder')" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="closedDialog">{{ $t("common.cancel") }}</el-button>
          <el-button type="primary" @click="submitForm">
            {{ $t("common.confirm") }}
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup lang="ts">
import { ref, reactive, onBeforeMount } from "vue";
import { type FormInstance, type FormRules, ElMessage } from "element-plus";
import type { FlowDelegateTaskRequest } from "@/api/flow/task/types";
import { type Entry } from "jmash-core";
import { taskApi } from "@/api/flow/task/index";
import { useI18n } from "vue-i18n";
const { t } = useI18n();

const taskFormRef = ref<FormInstance>();
const rules = reactive<FormRules<FlowDelegateTaskRequest>>({
  userId: [
    {
      required: true,
      message: t("task.userId"),
      trigger: "change",
    },
  ],
  comment: [
    {
      required: true,
      message: t("task.comment"),
      trigger: "change",
    },
  ],
});

const props = defineProps({
  taskId: {
    type: String,
    default: "",
  },
});

const taskFrom = ref<FlowDelegateTaskRequest>({
  requestId: Math.random() + "task",
  taskId: props.taskId,
});
const emits = defineEmits(["refresh"]);

// 提交表单校验
const submitForm = async () => {
  await taskFormRef.value?.validate((valid) => {
    if (valid) {
      taskApi.delegateTask(taskFrom.value).then((res) => {
        if (res.status === 200) {
          ElMessage({
            message: t("common.submitSuccess"),
            type: "success",
          });
          emits("refresh", res.status === 200);
          closedDialog();
        }
      });
    }
  });
};

const model = defineModel<boolean>();
// 关闭弹框
function closedDialog() {
  if (model.value) {
    model.value = false;
  }
}
</script>
